﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
using UnityEngine.UI;

public class GameManager : MonoBehaviour
{
    public static GameManager Instance;

    private ArtNetDMXController1 ArtNet1;
    private byte[] data1 = new byte[512];

    private ArtNetDMXController2 ArtNet2;
    private byte[] data2 = new byte[512];

    public Text DMXInfoText;

    private bool isStart = false;

    private void Awake()
    {
        Instance = this;
    }

    private void Start()
    {
        ArtNet1 = GetComponent<ArtNetDMXController1>();
        data1 = new byte[512];

        ArtNet2 = GetComponent<ArtNetDMXController2>();
        data2 = new byte[512];

        for (int i = 0; i < data1.Length; i++)
        {
            data1[i] = (byte)(i / 2);
        }

        for (int i = 0; i < data2.Length; i++)
        {
            data2[i] = (byte)(i / 2);
        }

        StartCoroutine(InitGameData());
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.O))
        {
            SendDMXMsg(5, 255);
        }
        if (Input.GetKeyDown(KeyCode.C))
        {
            SendDMXMsg(5, 0);
        }

        if (Input.GetKeyDown(KeyCode.Alpha1))
        {
            SendDMXMsg(8, 1);
        }
        if (Input.GetKeyDown(KeyCode.Alpha2))
        {
            SendDMXMsg(8, 9);
        }
        if (Input.GetKeyDown(KeyCode.Alpha3))
        {
            SendDMXMsg(8, 17);
        }
        if (Input.GetKeyDown(KeyCode.Alpha4))
        {
            SendDMXMsg(8, 25);
        }
        if (Input.GetKeyDown(KeyCode.Alpha5))
        {
            SendDMXMsg(8, 33);
        }
        if (Input.GetKeyDown(KeyCode.Alpha6))
        {
            SendDMXMsg(8, 41);
        }
        if (Input.GetKeyDown(KeyCode.Alpha7))
        {
            SendDMXMsg(8, 49);
        }
        if (Input.GetKeyDown(KeyCode.Alpha8))
        {
            SendDMXMsg(8, 57);
        }
    }

    private IEnumerator InitGameData()
    {
        yield return new WaitForSeconds(3.0f);
        Init();
        isStart = true;
    }

    private void Init()
    {
        //SendDMXMsg(1, 255);
        //SendDMXMsg(2, 0);
        //SendDMXMsg(3, 0);
        //SendDMXMsg(4, 0);
        //SendDMXMsg(5, 255);
        Debug.Log("初始化灯带数据！！！");
    }


    private void SendDMXMsg(int channel,byte volue)
    {
        data1[channel-1] = volue;
        ArtNet1.Send(data1);
    }
}
